package cn.liziguo.tank.script;

import cn.liziguo.tank.config.GameConfig;
import cn.liziguo.tank.constants.BulletType;
import cn.liziguo.tank.constants.CampEnum;
import cn.liziguo.tank.constants.PropertyConst;
import cn.liziguo.tank.factory.EffectsFactory;
import cn.liziguo.tank.util.Time;
import cn.liziguo.tank.vector.Vector2;
import com.almasb.fxgl.core.math.Vec2;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.component.Component;

/**
 * @author Liziguo
 * @date 2022-07-09 17:21:57
 */
public final class BulletComponent extends Component {

    /**
     * 子弹等级
     */
    public final int level;

    /**
     * 子弹的伤害
     */
    public final int attack;

    /**
     * 子弹飞行速度
     */
    public final double speed;

    /**
     * 子弹飞行方向
     */
    public final Vec2 vector;

    public BulletComponent(BaseTankComponent baseTankComponent) {
        level = baseTankComponent.bulletLevel;
        speed = 400 + baseTankComponent.bulletLevel * 100;
        vector = baseTankComponent.vector;
        attack = level * 2;
    }

    public BulletComponent(int level, int attack, double speed, Vec2 vector) {
        this.level = level;
        this.attack = attack;
        this.speed = speed;
        this.vector = vector;
    }

    @Override
    public void onAdded() {
        Vector2.setRotation(vector, entity);
    }

    @Override
    public void onUpdate(double tpf) {
        entity.translate(vector.mul(Time.deltaTime * speed));
    }

    public static void onRemove(Entity bullet) {
        if (bullet.getObject(PropertyConst.BULLET_TYPE) == BulletType.NORMAL) {
            EffectsFactory.createBoom(bullet.getCenter());
        } else {
            boolean camp = bullet.getObject(PropertyConst.CAMP) == CampEnum.PLAYER;
            EffectsFactory.createBigBoom(bullet.getCenter(), GameConfig.BULLET_PLUS_BOOM_SIZE, camp, GameConfig.BULLET_PLUS_BOOM_ATTACK);
        }
        bullet.removeFromWorld();
    }

}
